package org.example.rabbitmqdemo.controller;

import org.example.rabbitmqdemo.constant.Constants;
import org.example.rabbitmqdemo.model.User;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/producer")
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/work")
    public String work(){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE, "hello——" + i);
        }
        return "发送成功～";
    }

    @RequestMapping("/fanout")
    public String fanout(){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE, "", "hello——" + i);
        }
        return "发送成功～";
    }

    @RequestMapping("/object")
    public String objectSend(){
        for (int i = 0; i < 10; i++) {
            Message message = new Message("nihao".getBytes(), new MessageProperties());
            message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            User user = new User(i, "test--" + i, 10 + i);
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE, user);
        }
        return "发送成功～";
    }
}
